# GCC路径
GCC_PATH = /usr/bin
PWD=`pwd`

# 目标名字
TARGET = STM32F103C8T6

BUILD_DIR = build

RTOS ?= ../../kernel/FreeRTOS
HAL ?= ../../bsp

######################################################
#         HAL层代码
######################################################
C_SOURCES = \
	$(HAL)/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \
	$(HAL)/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \
	$(HAL)/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \
	$(HAL)/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \
	$(HAL)/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \
	$(HAL)/CMSIS/CM3/CoreSupport/core_cm3.c \
	$(HAL)/CMSIS/Device/ST/STM32F1xx/Source/Templates/system_stm32f1xx.c

C_INCLUDES =  \
	-I$(HAL)/CMSIS/CM3/Include \
	-I$(HAL)/CMSIS/CM3/CoreSupport \
	-I$(HAL)/CMSIS/Device/ST/STM32F1xx/Include \
	-I$(HAL)/STM32F1xx_HAL_Driver/Inc \
	-I../../Application \

C_DEFS = \
	-DUSE_HAL_DRIVER \
	-DSTM32F103x6

######################################################
#        RTOS
######################################################
C_SOURCES += \
	$(RTOS)/tasks.c \
	$(RTOS)/list.c \
	$(RTOS)/queue.c \
	$(RTOS)/timers.c \
	$(RTOS)/portable/GCC/ARM_CM3/port.c \
	$(RTOS)/portable/MemMang/heap_4.c

C_INCLUDES +=  \
	-I$(RTOS)/include \
	-I$(RTOS)/portable/GCC/ARM_CM3 \
	-I$(RTOS)

######################################################
#        Application
######################################################
C_SOURCES += \
	../../Application/main.c \
	../../Application/stm32f1xx_hal_msp.c \
	../../Application/stm32f1xx_it.c \
	../../Application/system.c

# ASM sources
ASM_SOURCES =  \
	$(HAL)/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/startup_stm32f103x6.s

# debug build?
DEBUG = 1
# optimization
OPT = -Os

# gcc tools
PREFIX = arm-none-eabi-
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

# fpu
# NONE for Cortex-M0/M0+/M3
# float-abi
# mcu
MCU = -mcpu=cortex-m3

# AS includes
AS_INCLUDES = \
	-I$(RTOS)\Source

# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -fmessage-length=0
CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -fmessage-length=0

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"


# link script
LDSCRIPT = STM32F103C8Tx_FLASH.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map -Wl,--cref -Wl,--gc-sections -Wl,--print-memory-usage

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin


# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@

$(BUILD_DIR):
	mkdir $@


run:
	qemu-system-arm -M stm32f103-taurus \
		-kernel build/STM32F103C8T6.elf \
		-serial stdio \
		-show-cursor

debug:
	qemu-system-arm -M stm32f103-taurus \
		-kernel build/STM32F103C8T6.elf \
		-serial stdio \
		-show-cursor \
		-S -s

clean:
	-rm -fR $(BUILD_DIR)

# dependencies
-include $(wildcard $(BUILD_DIR)/*.d)
